<template>
  <!-- 业务组件配置管理 -->
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar :add-active="addActive" :del-active="delActive" :cancel-active="cancelActive" :has-edit="false"
        :has-del="false" :has-save="false" :save-active="saveActive" @add="add" @cancel="cancel" @del="del" />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">基地名称:</div>
          <div class="input">
            <el-select v-model="organizationNo" value-key="enum" learable class="selcect-one"
              @change="gettabledataList()">
              <el-option v-for="(comitem, comindex) in organizationList" :key="comindex" :value="comitem.organizationNo"
                :label="comitem.organizationName" />
            </el-select>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">农业类型:</div>
          <div class="input">
            <el-select v-model="farmingTypeNo" value-key="enum" filterable clearable class="selcect-one"
              @change="gettabledataList()">
              <el-option v-for="(comitem, comindex) in certificatesList" :key="comindex" :value="comitem.farmingTypeNo"
                :label="comitem.farmingTypeName" />
            </el-select>
          </div>
        </div>
        <el-button @click="encryption">生成加密跳转链接</el-button>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <!-- 业务组件展示表格 -->
      <div class="zh-table-view">
        <el-table :data="tableData" style="width: 100%" @row-click="handleEdit" height="100%">
          <el-table-column type="index" width="50" />
          <el-table-column prop="organizationName" label="基地名称">
            <template slot-scope="scope" v-if="scope.row.organizationNo">
              {{ organizationName(scope.row.organizationNo) }}
            </template>
          </el-table-column>
          <el-table-column prop="bigScreenName" label="大屏名称" />
          <el-table-column prop="url" label="跳转链接地址" />
          <el-table-column prop="picUrl" label="大屏图片">
            <template slot-scope="scope">
              <el-image
                v-if="scope.row.picUrl"
                style="height: 50px"
                :src="scope.row.picUrl"
                :preview-src-list="[scope.row.picUrl]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="edit">修改</el-button>
              <el-button size="mini" type="warning" @click="dellineallmes(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
          @changePageSize="changePageSize" @changeCurrentPage="changeCurrentPage" />
      </div>
    </template>

    <template v-slot:dialog>
      <!-- 新增业务组件的弹框 -->
      <el-dialog :title="titleText + '大屏跳转链接'" class="formData" :visible.sync="dialogFormVisible" width="50%" top="5vh"
        :before-close="() => { dialogFormVisible = false }">
        <el-form ref="formData" label-position="left" :model="formData" :rules="rules" class="el-form">
          <el-form-item label="选择基地:" prop="organizationNo" class="w100p">
            <el-select v-model="formData.organizationNo" value-key="organizationNo" filterable clearable>
              <el-option v-for="(comitem, comindex) in organizationList" :key="comindex" :value="comitem.organizationNo"
                :label="comitem.organizationName" />
            </el-select>
          </el-form-item>
          <el-form-item label="选择农业类型:" prop="type" class="w100p">
            <el-select v-model="formData.type" value-key="farmingTypeNo" filterable clearable>
              <el-option v-for="(comitem, comindex) in certificatesList" :key="comindex" :value="comitem.farmingTypeNo"
                :label="comitem.farmingTypeName" />
            </el-select>
          </el-form-item>
          <el-form-item label="农业类型编码:" prop="type" class="w100p">
            <el-input v-model="formData.type" clearable />
          </el-form-item>
          <el-form-item label="	大屏名称:" prop="bigScreenName" class="w100p">
            <el-input v-model="formData.bigScreenName" clearable />
          </el-form-item>
          <el-form-item label="跳转链接地址:" prop="url" class="w100p">
            <el-input v-model="formData.url" clearable />
          </el-form-item>
          <el-form-item label="大屏图片:" prop="url" class="w100p">
            <upload :limit="1" :urlList="formData.urlList" @getFile="getFile" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="savemes">保 存</el-button>
        </div>
      </el-dialog>
      <el-dialog :title="'跳转链接加密账号密码'" class="formData" :visible.sync="dialogEncryptiFormVisible" width="50%" top="5vh"
        :before-close="() => { dialogEncryptiFormVisible = false }">
        <el-form ref="formData" label-position="left" :model="formData" :rules="rules" class="el-form">
          <el-form-item label="账号:" prop="username" class="w100p">
            <el-input v-model="formEncData.username" clearable />

          </el-form-item>
          <el-form-item label="密码:" prop="password" class="w100p">
            <el-input v-model="formEncData.password" clearable />
          </el-form-item>
          <el-form-item label="跳转链接地址:" prop="url" class="w100p">
            <el-input v-model="formEncData.url" clearable
              placeholder="https://matefarm.net/agricultural-management-platform/#/login-loading?token=" />
          </el-form-item>
          <el-form-item prop="url" class="w100p">
            <el-button @click="encry">生成(只支持管理端)</el-button>
            <div>{{ url }}</div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEncryptiFormVisible = false">关 闭</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>
<script>
import ActionBar from "@/components/ActionBar/index";
import { mymixin } from "@/utils/mixins";
import SearchBox from "@/components/SearchBox";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
// import zhreq from '@/api/basic-info-manage/warehouse-management'
import Pagination from "@/components/Pagination";
import {
  getencryptionCookie
} from "@/utils/cookie";
import upload from "@/components/uploadimage/upload.vue";
import axios from 'axios';
// import zhreq from '@/api/basic-info-manage/category-management'
// import zhreqs from '@/api/basic-info-manage/breed-varieties-management'
// import { getTitleList } from "@/components/getnameurl";
export default {
  components: { ActionBar, Pagination, BaseMainSlot, SearchBox, upload },
  mixins: [mymixin],
  data() {
    return {
      isshowuploadpic: false,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      totalSize: 0,
      // ---添加相关---
      diastau: 0,
      formData: {
      },
      selectStau: '',
      organizationList: [],
      warehouseSystem: undefined,
      certificatesList: [],
      warehouseName: undefined,
      // organizationName: undefined,
      databaseName: '',
      organizationNo: '',
      farmingTypeNo: '',
      categoryList: [],
      VarietyList: [],
      postUrl: '',
      rules: {
        organizationNo: [
          { required: true, message: '请选择基地', trigger: 'change' }
        ],
        imgTypeNo: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        warehouseSystem: [
          { required: true, message: '请选择系统', trigger: 'change' }
        ]
      },
      plantTypeNo: '',
      varietyNo: '',
      dialogEncryptiFormVisible: false,
      formEncData: {
        url: `${this.$serviceUrl}/agricultural-management-platform/#/login-loading?token=`
      },
      url: ''
    }
  },
  created() {
    // this.goback()
    this.gettabledataList()
  },
  mounted() {
    this.queryDataList()
    // this.gettabledataList()
  },
  methods: {
    // 上传图片
    getFile (file) {
      this.formData.picUrl = file[0] ? file[0].url : ''
    },
    encryption() {
      this.dialogEncryptiFormVisible = true
    },
    encry() {
      let str = getencryptionCookie(this.formEncData.username, this.formEncData.password)
      this.url = this.formEncData.url + str
    },
    // 获取下拉框信息
    async queryDataList() {
      const { data: res } = await axios({
        method: 'post',
        url: this.$webSerUrlPro + 'organization/organization/queryByCondition',
        headers: {
        },
        data: {}
      })
      this.organizationList = res.data
      const { data: mes } = await axios({
        method: 'post',
        url: this.$webSerUrlPro + 'organization/organizationFarmingTypeMapper/queryByCondition',
        headers: {
        },
        data: {}
      })
      this.certificatesList = mes.data
    },
    organizationName(e) {
      let index = this.organizationList.findIndex(item => item.organizationNo === e)
      // console.log(organizationName)
      return index != -1 ? this.organizationList[index].organizationName : ''
    },
    // 获取表格信息
    async gettabledataList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          organizationNo: this.organizationNo || undefined,
          type: this.farmingTypeNo || undefined,
        }
      }
      const { data: mes } = await axios({
        method: "post",
        url: this.$webSerUrlPro + 'organization/organizationBigScreen/queryPageList',
        headers: {
          'organizationList': this.organizationList,
          'organizationNo': this.organizationNo
        },
        data: params
      })
      if (mes.code === 1) {
        this.tableData = mes.data.content || []
        this.totalSize = mes.data.totalElements
        this.resetActionBar()
      } else {
        this.$message.warning("获取表格失败")
      }
    },
    // 搜索
    search() {
      this.currentPage = 1;
      this.gettabledataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.gettabledataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.gettabledataList();
    },
    // 图片
    childEvent(e) {
      // console.log(e)
      this.$set(this.formData, 'imageData', e)
    },
    // 删除一个业务组件
    dellineallmes(i, e) {
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios({
          method: 'get',
          url: this.$webSerUrlPro + 'organization/organizationBigScreen/del?organizationBigScreenId=' + e.organizationBigScreenId,
          headers: {
            'organizationNo': this.organizationNo,
            'organizationList': this.organizationList,
            'databaseName': this.databaseName
          }
        }).then(res => {
          if (res.data.code === 1) {
            this.diastau = 0
            this.$message.success("删除成功！")
            this.gettabledataList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleEdit(row) {
      if (row) {
        this.formData = {
          ...row,
          urlList: row.picUrl ? [{url: row.picUrl}] : []
        }
        this.postUrl = row.imgData
      }
    },
    // 修改一个业务组件
    editlineallmes(i, e) {
      if (e) {
        this.formData = JSON.parse(JSON.stringify(e))
        this.diastau = 3
      }
    },
    // 添加
    add() {
      this.canInput = true;
      this.nowChanceStau = 'add'
      this.formData = {
      }
      this.titleText = "添加"
      this.dialogFormVisible = true;
      this.addActive = false;
      this.delActive = false;
      this.saveActive = true;
      this.cancelActive = true;
      this.editActive = false;
    },
    // 保存要添加的业务的组件信息
    savemes() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            let param = {
              ...this.formData
            }
            axios({
              method: "post",
              url: this.$webSerUrlPro + 'organization/organizationBigScreen/edit',
              headers: {
                'organizationNo': this.organizationNo
              },
              data: param
            }).then(res => {
              if (res.data.code === 1) {
                // this.diastau = 0
                this.$message.success("修改成功！")
                this.gettabledataList()
              }
            })
            // zhreq.editList(param).then(response => {
            //   let mes = response
            // })
          } else if (this.nowChanceStau === "add") {
            let param = {
              ...this.formData
            }
            axios({
              method: "post",
              url: this.$webSerUrlPro + 'organization/organizationBigScreen/add',
              headers: {
                'organizationNo': this.organizationNo
              },
              data: param
            }).then(res => {
              if (res.data.code === 1) {
                this.$message.success("添加成功！")
                this.gettabledataList()
              }
            })
            // zhreq.addList(param).then(response => {
            //   let mes = response
            //   if (mes.code === 1) {
            //     this.$message.success("添加成功！")
            //     this.gettabledataList()
            //   }
            // })
          }
          // alert('submit!');
        } else {
          // console.log('error submit!!');
          return false;
        }
      })
    },
    // 修改时-保存要添加的业务的组件信息
    savemesedit(e, v) {
      // console.log(e, v)
      let param = {
        // ...this.formData
        auditFlag: e,
        warehouseConfigNo: v.warehouseConfigNo,
        organizationNo: this.organizationNo
      }
      axios({
        method: "post",
        url: this.$webSerUrlPro + 'warehouse/warehouse/edit',
        headers: {
          'organizationList': this.organizationList,
          'organizationNo': this.organizationNo,
          'databaseName': this.databaseName
        },
        data: param
      }).then(res => {
        // console.log(res)
        if (res.data.code === 1) {
          this.$message.success("修改成功！")
          this.gettabledataList()
        } else {
          this.$message.success(res.data.msg)

          return false
        }
      })
      // zhreq.editList(param).then(response => {
      //   let mes = response
      //   if (mes.code === 1) {
      //     this.diastau = 0
      //     this.$message.success("修改成功！")
      //     this.gettabledataList()
      //   }
      // })
    },
    // 获取状态
    getstau(e) {
      this.selectStau = e
      if (e === 'add') {
        this.diastau = 1
      } else if (e === 'complete') {
        this.diastau = 0
      } else if (e === 'edit') {
        this.diastau = 3
      }
    },
    // 设置弹框展示
    setdiaview(i) {
      this.diastau = i
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.business-main {
  width: 100%;
  height: 100%;

  .zh-table-view {
    width: 100%;
    height: calc(100% - 60px);
    position: relative;
  }
}

.add-components-dia {
  width: 502px;
  // height: 191px;
  background: #303640;
  border: 1px solid #005c6f;
  border-radius: 7px;
  position: fixed;
  top: 15%;
  left: 40%;
  // background-color: #005c6f;
  z-index: 99;

  .screen-dia-main {
    width: 502px;
    padding: 20px 10px;
    // max-height: 550px;
    overflow-y: auto;
    overflow-x: hidden;
    max-height: 480px;

    .dia-main-inputline {
      width: 482px;
      margin-top: 5px;
      display: flex;
      // justify-content: center;
      align-items: center;
      margin-bottom: 10px;

      .selcect-one {
        width: 350px;
        height: 32px;
        box-sizing: border-box;
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
        border: 1px solid #494e57;
        box-sizing: border-box;
        outline: 0;
      }
    }

    .dia-main-addline {
      width: 442px;
      font-size: 16px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: $textColor;
      line-height: 24px;
      border-bottom: #00faad 1px dashed;
      padding-bottom: 16px;
      margin-bottom: 10px;

      .components-line {
        display: flex;
        // justify-content: space-between;
        align-items: center;
        width: 442px;
        height: 48px;

        .selcect-one {
          width: 350px;
          color: #00faad;
          margin-top: 12px;
          height: 36px;
          line-height: 36px;
          background-color: transparent;
          border: #00faad dashed 1px;
          cursor: pointer;

          option {
            background-color: #005c6f !important;
            color: $textColor;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>
